//
//  RetweetedView.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/19.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

class RetweetedView: UIView {

    /// 微博数据的视图模型
    var viewModel: StatusViewModel? {
        
        didSet {
            
            //设置转发微博文字
            if let retweetedText = viewModel?.retweetedText {
                
                statusLabel.text = retweetedText
            }
            
            //设置配图视图的数据模型
            pictureView.pic_urls = viewModel?.status.retweeted_status?.pic_urls
            pictureView.viewModel = viewModel
            
            //如果没有转发微博，则把offset设成为0，否则设成seprator
            let labelOffset = viewModel?.status.retweeted_status == nil ? 0 :cellLayout.seprator
            
            statusLabel.snp_updateConstraints { (make) -> Void in
                
                make.top.equalTo(self).offset(labelOffset)
            }
            
            //如果没有图片视图，则把offset设成0,否则设成seprator
            var pictureOffset: CGFloat = 0
            
            if let retweetedModel = viewModel?.status.retweeted_status, pic_urls = retweetedModel.pic_urls where pic_urls.count > 0 {
                
                pictureOffset = cellLayout.seprator
            }
            
            pictureView.snp_updateConstraints { (make) -> Void in
                
                make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout.seprator)
                make.left.equalTo(statusLabel)
                make.size.equalTo((viewModel?.retweetedPictureViewSize)!)
                make.bottom.equalTo(self).offset(-cellLayout.seprator)
            }
        }
    }
    
    ///微博文字
    let statusLabel = UILabel(text: "", fontSize: 15)
    
    //配图视图
    let pictureView = PictureView()
    
    init() {
        
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 设置UI
extension RetweetedView {
    
    func setupUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //添加子视图
        addSubview(statusLabel)
        
        //添加配图视图
        addSubview(pictureView)
        
        //自动布局
        statusLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(cellLayout.seprator)
            make.top.equalTo(self).offset(cellLayout.seprator)
            make.right.equalTo(self).offset(-cellLayout.seprator)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout.seprator)
            make.left.equalTo(statusLabel)
            make.size.equalTo(CGSizeZero)
            make.bottom.equalTo(self).offset(-cellLayout.seprator)
        }
    }
}
